<?php
declare (strict_types=1);

namespace app\admin\controller\tally;

use app\common\model\TallyProgressDay;
use app\common\model\TallyProgressMonth;
use app\common\model\TallyTodo;
use ffhome\common\util\DateUtil;
use ffhome\frame\service\DictDataService;

class ProgressDay extends AbstractProgressDay
{
    protected function indexPage($param)
    {
        $param['d_progress_date_ge'] = date('Y-m-01');
        return parent::indexPage($param);
    }

    protected function editPage(array $row)
    {
        $this->assign('todoList', TallyTodo::getListByDayId($row['id']));
        $this->assign('monthList', TallyProgressMonth::getMonthListByDayId($row['id']));
        return parent::editPage($row);
    }

    protected function onBeforeEdit(array &$data, array $row)
    {
        if (!empty($data['progress_date'])) {
            if (TallyProgressDay::countByAccountingIdAndDate($row['accounting_id'], $data['progress_date']) > 0) {
                $this->error(lang('day.repeat_process_date'), $row);
            }
        }
        parent::onBeforeEdit($data, $row);
    }

    protected function onBeforeDelete($id): array
    {
        // 判断存在日工作表
        if (TallyTodo::findByDayId($id)) {
            $this->error(lang('month.error_exist_todo'));
        }
        return parent::onBeforeDelete($id);
    }

    protected function getExportHeader()
    {
        return [
            [lang('accounting.name'), 'accounting_name'],
            [lang('common.type'), 'type'],
            [lang('day.progress_date'), 'progress_date'],
            [lang('day.content'), '_content'],
            [lang('common.estimate_time_alias'), 'estimate_time'],
            [lang('common.actual_time_alias'), 'actual_time'],
            [lang('day.completion_status'), 'completion_status'],
            [lang('common.appraise'), 'appraise'],
            [lang('common.update_time'), 'update_time'],
        ];
    }

    protected function convertExportData(&$list)
    {
        foreach ($list as &$vo) {
            $vo['progress_date'] = $vo['progress_date'] . '[' . DateUtil::week($vo['progress_date']) . ']';
            $vo['type'] = DictDataService::getNameByValue($vo['type'], 'day_type_list');
            $vo['completion_status'] = DictDataService::getNameByValue($vo['completion_status'], 'completion_status_list');
            $vo['appraise'] = DictDataService::getNameByValue($vo['appraise'], 'appraise_list');
        }
    }

    public function generateTomorrowPlan()
    {
        $id = TallyProgressDay::generateTomorrowPlan();
        $this->success(lang('day.generate_success_info'), ['id' => $id]);
    }
}
